package com.scu.phototabuser.ui.login;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.test.espresso.core.deps.guava.base.Strings;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;

import com.google.gson.Gson;
import com.scu.phototabuser.Consts;
import com.scu.phototabuser.R;
import com.scu.phototabuser.data.source.remote.Response;
import com.scu.phototabuser.http.OkHttp3Methods;
import com.scu.phototabuser.ui.taskgroup.ChooseGroupActivity;
import com.scu.phototabuser.util.ToastUtil;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by tianfei on 2017/5/15.
 */

public class FindPasswordActivity extends AppCompatActivity {


    @BindView(R.id.input_email) EditText input_email;
    @BindView(R.id.toolbar) Toolbar toolbar;
//    @BindView(R.id.send1email) TextView btn_send_findPwdEmail;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_password);
        ButterKnife.bind(this);

        toolbar.setTitle(getString(R.string.find_pwd));
        toolbar.setTitleTextColor(ContextCompat.getColor(FindPasswordActivity.this,R.color.colorAccent));
        setSupportActionBar(toolbar);
    }

    @OnClick(R.id.send1email)
    public void send1email(){

        final String email=input_email.getText().toString();
        if (!android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()){
            input_email.setError(getString(R.string.wrong_email_format));
            return;
        }

        //提示用户发送邮件//
        final AlertDialog.Builder alertBuilder=new AlertDialog.Builder(this);
        alertBuilder.setTitle(getString(R.string.hint));
        alertBuilder.setCancelable(true);
        alertBuilder.setMessage(getString(R.string.send_1_email));
        alertBuilder.setPositiveButton(getString(R.string.i_am_sure),
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String _email=input_email.getText().toString();
                        AsycSendEmailRequest(_email);
                    }
                });
        alertBuilder.setNegativeButton(getString(R.string.i_will_rethink),
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
        alertBuilder.show();
    }

    private void AsycSendEmailRequest(final String email) {

        Observable.create(new ObservableOnSubscribe<Void>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<Void> e) throws Exception {
                final Map<String,String> params=new HashMap<>();
                params.put("email",email);
                String json = OkHttp3Methods.PostRequestWithToken(Consts.SendFindPwdEmailURL, params);
                Response r=new Gson().fromJson(json,Response.class);
                if (r.getCode() == 200){
                    e.onComplete();
                }else {
                    e.onError(new RuntimeException(r.getMessage()));
                }

            }
        }).subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new Observer<Void>() {
            @Override
            public void onSubscribe(@NonNull Disposable d) {}

            @Override
            public void onNext(@NonNull Void aVoid) {}

            @Override
            public void onError(@NonNull Throwable e) {
                ToastUtil.makeToast(FindPasswordActivity.this,e.getMessage());
            }
            @Override
            public void onComplete() {
                ToastUtil.makeToast(FindPasswordActivity.this,"发送成功,请查看邮箱");
                FindPasswordActivity.this.onBackPressed();
            }
        });

    }
}
